<template>
  <Dialog v-model="dialogVisible" width="600px" title="添加站点" maxHeight="400px" @close="closeHandle">
    <el-form ref="ruleFormRef" :model="formData" :rules="formRules" label-width="80px" class="w-[500px]">
      <el-form-item label="域名" prop="domainName" required>
        <!--<el-tooltip class="box-item" effect="dark" placement="top-start">
           <template #content>
            <div class="">
              如需填写多个域名，请enter填写，每个选项是一个域名，默认为80端口
              <br>泛解析添加方法 *.domain.com
              <br>如另加端口格式为 www.domain.com:88
            </div>
          </template>
          <el-select v-model="formData.domainName" class="select-c w-[100%]" multiple filterable allow-create
            default-first-option :reserve-keyword="false" placeholder="请输入" @change="domainNameSelectHandle">
          </el-select> 
         </el-tooltip> -->
        <el-input
v-model="formData.domainName" type="text" placeholder="请输入" clearable maxlength="220"
          @input="domainNameInputHandle" />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input
v-model="formData.note" type="textarea" placeholder="请输入" clearable
          :autosize="{ minRows: 2, maxRows: 6 }" show-word-limit maxlength="220" />
      </el-form-item>

      <el-form-item label="根目录" prop="rootDirectory">
        <el-input
v-model="formData.rootDirectory" type="text" placeholder="请输入" clearable show-word-limit
          maxlength="220" />
      </el-form-item>
      <el-form-item label="FTP" prop="has_FTP" required>
        <el-select v-model="formData.has_FTP" class="select-c" clearable placeholder="请选择">
          <el-option v-for="item in hasFTPOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据库" prop="has_DB" required>
        <el-select v-model="formData.has_DB" class="select-c" clearable placeholder="请选择">
          <el-option v-for="item in hasDBOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="PHP版本" prop="PHP" required>
        <el-select v-model="formData.PHP" class="select-c" clearable placeholder="请选择">
          <el-option v-for="item in PHPOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="closeHandle">关闭</el-button>
      <el-button type="primary" :loading="loading" @click="saveHandle(ruleFormRef)">提交</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ref, computed, watch, defineEmits, reactive, onMounted } from 'vue'
import { addWebPhpWebAPI } from "@/api/systemMonitor/site"
import { propTypes } from '@/utils/propTypes'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['update:modelValue', 'confirm'])

const props = defineProps({
  modelValue: propTypes.bool.def(false),
})
const defaultRoot = '/duckup/duckWeb/root/'
//默认表单
const defaultFormData = {
  domainName: '',
  note: "",
  rootDirectory: defaultRoot,
  has_FTP: "",
  has_DB: "",
  PHP: "",
}

const loading = ref(false)

const formData = reactive({
  ...defaultFormData
})

const ruleFormRef = ref<FormInstance>()
const formRules = reactive<FormRules>({
  domainName: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  rootDirectory: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  has_FTP: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  has_DB: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  PHP: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
})
onMounted(() => {
})


//弹出框状态
const dialogVisible = ref(props.modelValue)

let hasFTPOption = ref([{
  label: '不创建',
  value: '0',
}, {
  label: '创建',
  value: '1',
}])

let hasDBOption = ref([{
  label: '不创建',
  value: '0',
}, {
  label: '创建',
  value: '1',
}])
let PHPOption = ref([{
  label: '版本7.4',
  value: '74',
}])
watch(() => props.modelValue, (newVal) => {
  dialogVisible.value = newVal;
})
//提交
const saveHandle = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      loading.value = true;
      addWebPhpWebAPI({
        ...formData,
      }).then(() => {
        ElMessage.success("添加成功")
        closeHandle()
        emit('confirm')
      }).finally(() => {
        loading.value = false;
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
//关闭
const closeHandle = () => {
  dialogVisible.value = false;
  //初始化数据
  Object.assign(formData, defaultFormData);
  emit('update:modelValue', dialogVisible)
}

//域名change事件
const domainNameSelectHandle = (val) => {
  if (Object.prototype.toString.call(val) === '[object Array]' && val.length > 0) {
    formData.rootDirectory = `${defaultRoot}${val[0]}`;
    formData.note = val[0];
  } else {
    formData.rootDirectory = defaultRoot;
    formData.note = "";
  }
}
// 域名change事件
const domainNameInputHandle = (val) => {
  if (Object.prototype.toString.call(val) === '[object String]' && val.length > 0) {
    formData.rootDirectory = `${defaultRoot}${val}`;
    formData.note = val;
  } else {
    formData.rootDirectory = defaultRoot;
    formData.note = "";
  }
}
</script>

